package com.kedacom.freedomLabel.model

import android.app.PendingIntent
import android.app.Service
import android.arch.lifecycle.Observer
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbManager
import android.os.IBinder
import com.gprinter.command.EscCommand
import com.gprinter.command.LabelCommand
import com.gprinter.command.LabelCommand.DIRECTION
import com.kedacom.freedomLabel.MainApplication
import com.kedacom.freedomLabel.Options
import com.kedacom.freedomLabel.gpprinter.Constant.ACTION_USB_PERMISSION
import com.kedacom.freedomLabel.gpprinter.DeviceConnFactoryManager
import com.kedacom.freedomLabel.gpprinter.ThreadPool
import com.kedacom.freedomLabel.model.bean.*
import com.kedacom.freedomLabel.utils.log.LLog
import com.kedacom.lego.fast.util.ToastUtil
import com.kedacom.lego.fast.view.notice.INotice
import com.kedacom.lego.message.LegoEventBus
import com.kedacom.lego.message.LegoMessageManager
import java.text.SimpleDateFormat
import java.util.*

/**
 * Author：李程
 * CreateTime：2020/9/6 14:04
 * E-mail：licheng@kedacom.com
 * Describe：
 */
open class MyPrintService : Service() {

    private val tag: String = "GpPrintService"
    private var usbDeviceAddresses: ArrayList<UsbDevice> = ArrayList()
    var mUsbDevice: UsbDevice? = null
    private val mPrinterId = 0
    private var threadPool: ThreadPool = ThreadPool.getInstantiation()
//    private val mInputRecordObserver = Observer { record: PrintByIdName? ->
//        if (record != null) {
//            printRecord(record)
//        }
//    }
    private val mInputRecordObserver = Observer { record: PrintRecordBusiness? ->
        if (record != null) {
            printRecord(record)
        }
    }
    private val mUSBBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action ?: return
            mUsbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)
            when (action) {
                ACTION_USB_PERMISSION -> mUsbDevice?.let { device ->
                    if (!checkUsbDevicePidVid(device)) {
                        LLog.error(tag, "无法识别的USB设备")
                        return@let
                    }
                    usbConn(device)
                }
                UsbManager.ACTION_USB_DEVICE_ATTACHED -> mUsbDevice?.let { device ->
                    if (!checkUsbDevicePidVid(device)) {
                        LLog.error(tag, "无法识别的USB设备")
                        return@let
                    }
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        usbConn(device)
                    } else {
                        val pendingIntent = PendingIntent.getBroadcast(MainApplication.getApplication(), 0, Intent(ACTION_USB_PERMISSION), 0)
                        val manager = MainApplication.getApplication().getSystemService(Context.USB_SERVICE) as UsbManager
                        manager.requestPermission(device, pendingIntent)
                    }
                }
                UsbManager.ACTION_USB_DEVICE_DETACHED -> mUsbDevice?.let { device ->
                    if (!checkUsbDevicePidVid(device)) {
                        LLog.error(tag, "无法识别的USB设备")
                        return@let
                    }
                    closeport()
                }
                DeviceConnFactoryManager.ACTION_CONN_STATE -> {
                    val state = intent.getIntExtra(DeviceConnFactoryManager.STATE, -1)
                    val deviceId = intent.getIntExtra(DeviceConnFactoryManager.DEVICE_ID, -1)
                    when (state) {
                        DeviceConnFactoryManager.CONN_STATE_DISCONNECT -> if (mPrinterId == deviceId) {
                            ToastUtil.showFilteredToast(MainApplication.getApplication(), "打印机断开连接", INotice.TOAST_ERROR)
                            LegoMessageManager.getInstance().sendMessage(Options.MSG_PRINTER_STATUS, PrinterMessage(1, "打印机断开连接"))
                        }
                        DeviceConnFactoryManager.CONN_STATE_CONNECTING -> {
                            ToastUtil.showFilteredToast(MainApplication.getApplication(), "打印机正在连接中", INotice.TOAST_INFO)
                            LegoMessageManager.getInstance().sendMessage(Options.MSG_PRINTER_STATUS, PrinterMessage(0, "打印机正在连接中"))
                        }
                        DeviceConnFactoryManager.CONN_STATE_CONNECTED -> {
                            ToastUtil.showFilteredToast(MainApplication.getApplication(), "打印机已连接", INotice.TOAST_INFO)
                            LegoMessageManager.getInstance().sendMessage(Options.MSG_PRINTER_STATUS, PrinterMessage(0, "打印机已连接"))
                        }
                        DeviceConnFactoryManager.CONN_STATE_FAILED -> {
                            ToastUtil.showFilteredToast(MainApplication.getApplication(), "打印机连接失败", INotice.TOAST_ERROR)
                            LegoMessageManager.getInstance().sendMessage(Options.MSG_PRINTER_STATUS, PrinterMessage(1, "打印机连接失败"))
                        }
                    }
                }
            }
        }
    }

    /**
     * usb连接
     *
     * @param usbDevice
     */
    fun usbConn(usbDevice: UsbDevice) {
        DeviceConnFactoryManager.Build()
                .setId(mPrinterId)
                .setConnMethod(DeviceConnFactoryManager.CONN_METHOD.USB)
                .setUsbDevice(usbDevice)
                .setContext(this)
                .build()
        DeviceConnFactoryManager.getDeviceConnFactoryManagers()[mPrinterId].openPort()
    }

    /**
     * 重新连接回收上次连接的对象，避免内存泄漏
     */
    fun closeport() {
        val deviceConnFactoryManager = DeviceConnFactoryManager.getDeviceConnFactoryManagers()[mPrinterId]
        if (deviceConnFactoryManager?.mPort != null) {
            deviceConnFactoryManager.run {
                reader.cancel()
                mPort.closePort()
            }
            deviceConnFactoryManager.mPort = null
        }
    }

    override fun onCreate() {
        super.onCreate()
        registerUSBBroadcastReceiver()
//        LegoEventBus.use(Options.MSG_PRINT_NOW, PrintByIdName::class.java).observeForever(mInputRecordObserver)
        LegoEventBus.use(Options.MSG_PRINT_NOW, PrintRecordBusiness::class.java).observeForever(mInputRecordObserver)
    }

    override fun onBind(intent: Intent?): IBinder? {
        TODO("Not yet implemented")
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        getUsbDeviceList()
        if (usbDeviceAddresses.isNotEmpty()) {
            val device = usbDeviceAddresses[0]
            val manager = MainApplication.getApplication().getSystemService(Context.USB_SERVICE) as UsbManager
            if (manager.hasPermission(device)) {
                usbConn(device)
            } else {
                val pendingIntent = PendingIntent.getBroadcast(MainApplication.getApplication(), 0, Intent(ACTION_USB_PERMISSION), 0)
                manager.requestPermission(device, pendingIntent)
            }
        }
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(mUSBBroadcastReceiver)
//        LegoEventBus.use(Options.MSG_PRINT_NOW, PrintByIdName::class.java).removeObserver(mInputRecordObserver)
        LegoEventBus.use(Options.MSG_PRINT_NOW, PrintRecordBusiness::class.java).removeObserver(mInputRecordObserver)
    }

    private fun printRecord(record: PrintRecordBusiness) {
        threadPool.addSerialTask(Runnable {
            if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[mPrinterId] == null || !DeviceConnFactoryManager.getDeviceConnFactoryManagers()[mPrinterId].connState) {
                ToastUtil.showFilteredToast(MainApplication.getApplication(), "请连接打印机", INotice.TOAST_ERROR)
                LegoMessageManager.getInstance().sendMessage(Options.MSG_PRINTER_STATUS, PrinterMessage(1, "请连接打印机"))
                return@Runnable
            }
            DeviceConnFactoryManager.getDeviceConnFactoryManagers()[mPrinterId].sendDataImmediately(sendLabel(record))
        })
    }



        private fun sendLabel(record: PrintRecordBusiness): Vector<Byte> {


            if (record.template.type.contains("60*30")) {
                return  printSixToThree(record);
            } else if (record.template.type.contains("60*40")) {
                return  printSixToFour(record);
            } else {
                return  printSixToFour(record);
            }



    }
    private fun printSixToThreeById(record: PrintByIdName): Vector<Byte>{

        var directionSet = DIRECTION.FORWARD
        var width = 60
        var height = 40
        width = record.template.width
        height = record.template.height

        val resultId = -1
        val tsc = LabelCommand()
        tsc.addSize(width, height) // 设置标签尺寸，按照实际尺寸设置

        tsc.addGap(2) // 设置标签间隙，按照实际尺寸设置，如果为无间隙纸则设置为0

        tsc.addDirection(directionSet, LabelCommand.MIRROR.NORMAL) // 设置打印方向

        tsc.addReference(0, 0) // 设置原点坐标

        tsc.addTear(EscCommand.ENABLE.ON) // 撕纸模式开启

        tsc.addSpeed(LabelCommand.SPEED.SPEED2)
        tsc.addCls() // 清除打印缓冲区

        tsc.addText(20, 30, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                 record.titleName)
        tsc.addText(20, 80, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "重量:" + record.weight +record.weightUnit)
        tsc.addText(20, 130, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "扫码:" + record.scanzxing)
        tsc.addText(20, 180, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "时间:" + sdf1.format(record.dateStr))

        tsc.addQRCode(280, 30, LabelCommand.EEC.LEVEL_M, 4, LabelCommand.ROTATION.ROTATION_0,
                record.titleName +
                        "$" + record.weight +record.weightUnit+
                        "$" + record.scanzxing +
                        "$" + sdf1.format(record.dateStr))

        tsc.addPrint(1, 1) // 打印标签

        tsc.addSound(2, 100) // 打印标签后 蜂鸣器响
        return tsc.command
    }
    private fun printSixToFourById(record: PrintByIdName): Vector<Byte>{

        var directionSet = DIRECTION.FORWARD
        var width = 60
        var height = 40
        width = record.template.width
        height = record.template.height

        val resultId = -1
        val tsc = LabelCommand()
        tsc.addSize(width, height) // 设置标签尺寸，按照实际尺寸设置

        tsc.addGap(2) // 设置标签间隙，按照实际尺寸设置，如果为无间隙纸则设置为0

        tsc.addDirection(directionSet, LabelCommand.MIRROR.NORMAL) // 设置打印方向

        tsc.addReference(0, 0) // 设置原点坐标

        tsc.addTear(EscCommand.ENABLE.ON) // 撕纸模式开启

        tsc.addSpeed(LabelCommand.SPEED.SPEED2)
        tsc.addCls() // 清除打印缓冲区

        tsc.addText(30, 75, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                record.titleName)
        tsc.addText(30, 135, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "重量:" + record.weight +record.weightUnit)

        tsc.addText(30, 195, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "扫码:" + record.scanzxing)
        tsc.addText(30, 255, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "时间:" + sdf1.format(record.dateStr))

        tsc.addQRCode(290, 50, LabelCommand.EEC.LEVEL_M, 5, LabelCommand.ROTATION.ROTATION_0,
                record.titleName +
                        "$" + record.weight +record.weightUnit+
                        "$" + record.scanzxing +
                        "$" + sdf1.format(record.dateStr))

        tsc.addPrint(1, 1) // 打印标签

        tsc.addSound(2, 100) // 打印标签后 蜂鸣器响
        return tsc.command
    }

    private fun printSixToFour(record: PrintRecordBusiness): Vector<Byte>{
        var printShowBeanList: MutableList<PrintShowBean> = ArrayList()
        printShowBeanList.clear()
        printShowBeanList = Assemblyprinting(record.business, record.columnNames,record.booleansNames) as MutableList<PrintShowBean>

        var directionSet = DIRECTION.FORWARD
        var width = 60
        var height = 40
        width = record.template.width
        height = record.template.height

        if (record.direction == "1") {
            directionSet = DIRECTION.BACKWARD
        }

        val tsc = LabelCommand()
        tsc.addSize(width, height) // 设置标签尺寸，按照实际尺寸设置

        tsc.addGap(2) // 设置标签间隙，按照实际尺寸设置，如果为无间隙纸则设置为0

        tsc.addDirection(directionSet, LabelCommand.MIRROR.NORMAL) // 设置打印方向

        tsc.addReference(0, 0) // 设置原点坐标

        tsc.addTear(EscCommand.ENABLE.ON) // 撕纸模式开启

        tsc.addSpeed(LabelCommand.SPEED.SPEED2)
        tsc.addCls() // 清除打印缓冲区


        val business =record.business;
        val dao =record.dao;

        if (dao != null) {
            val configVal = dao.get(ConfigKeys.COMPANY_NAME);
            if(configVal.size!=0){
                tsc.addText(20, 20, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                        dao.get(ConfigKeys.COMPANY_NAME).get(0).configValue)
//                                +
//                                dao.get(ConfigKeys.COMPANY_ADDRESS).get(0).configValue +
//                                dao.get(ConfigKeys.COMPANY_CONTACT_NUMBER).get(0).configValue)
            }

        }

        tsc.addText(20, 50, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "产品:" + business.name)
        tsc.addText(20, 80, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "操作人:" + business.operator_name)
        tsc.addText(260, 50, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "净重（kg）:")
        tsc.addText(260, 80, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                business.netWeight)
        if (printShowBeanList.size > 0) {
            for (j in printShowBeanList.indices) {
                if (110 + 30 * j < 250) {
                    var showuuName = ""
                    if (printShowBeanList[j].uuNameValue != null) {
                        showuuName = printShowBeanList[j].uuNameValue
                    }
                    tsc.addText(20, 110 + 30 * j, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                            printShowBeanList[j].uuName + ":" + showuuName)
                }
            }
        }

        tsc.addQRCode(300, 160, LabelCommand.EEC.LEVEL_M, 5, LabelCommand.ROTATION.ROTATION_0, " www.gprinter.com.cn")

        if (dao != null) {
            val configValSite = dao.get(ConfigKeys.COMPANY_INTERNET_SITE);
            if(configValSite.size!=0) {
                tsc.addText(20, 280, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                        "网址:" + dao.get(ConfigKeys.COMPANY_INTERNET_SITE).get(0).configValue)
            }
        }
        tsc.addPrint(1, 1) // 打印标签
        tsc.addSound(2, 100) // 打印标签后 蜂鸣器响
        return tsc.command
    }
    private fun printSixToThree(record: PrintRecordBusiness): Vector<Byte>{
        var printShowBeanList: MutableList<PrintShowBean> = ArrayList()
        printShowBeanList.clear()
        printShowBeanList = Assemblyprinting(record.business, record.columnNames,record.booleansNames) as MutableList<PrintShowBean>

        var directionSet = DIRECTION.FORWARD
        var width = 60
        var height = 40
        width = record.template.width
        height = record.template.height

        if (record.direction == "1") {
            directionSet = DIRECTION.BACKWARD
        }

        val resultId = -1
        val tsc = LabelCommand()
        tsc.addSize(width, height) // 设置标签尺寸，按照实际尺寸设置

        tsc.addGap(2) // 设置标签间隙，按照实际尺寸设置，如果为无间隙纸则设置为0

        tsc.addDirection(directionSet, LabelCommand.MIRROR.NORMAL) // 设置打印方向

        tsc.addReference(0, 0) // 设置原点坐标

        tsc.addTear(EscCommand.ENABLE.ON) // 撕纸模式开启

        tsc.addSpeed(LabelCommand.SPEED.SPEED2)
        tsc.addCls() // 清除打印缓冲区

        val business =record.business;
        val dao =record.dao;
        if (dao != null) {
            val configVal = dao.get(ConfigKeys.COMPANY_NAME);
            if(configVal.size!=0){
                tsc.addText(20, 20, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                        dao.get(ConfigKeys.COMPANY_NAME).get(0).configValue)
//                                +
//                                dao.get(ConfigKeys.COMPANY_ADDRESS).get(0).configValue +
//                                dao.get(ConfigKeys.COMPANY_CONTACT_NUMBER).get(0).configValue)
            }

        }

        tsc.addText(20, 50, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "产品:" + business.name)
        tsc.addText(20, 80, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "操作人:" + business.operator_name)
        tsc.addText(260, 20, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "净重（kg）:")
        tsc.addText(260, 50, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                business.netWeight)
        if (printShowBeanList.size > 0) {
            for (j in printShowBeanList.indices) {
                if (110 + 30 * j < 200) {
                    var showuuName = ""
                    if (printShowBeanList[j].uuNameValue != null) {
                        showuuName = printShowBeanList[j].uuNameValue
                    }
                    tsc.addText(20, 110 + 30 * j, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                            printShowBeanList[j].uuName + ":" + showuuName)
                }
            }
        }

        tsc.addQRCode(300, 120, LabelCommand.EEC.LEVEL_M, 4, LabelCommand.ROTATION.ROTATION_0, " www.gprinter.com.cn")
        if (dao != null) {
            val configValSite = dao.get(ConfigKeys.COMPANY_INTERNET_SITE);
            if(configValSite.size!=0) {
                tsc.addText(20, 200, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                        dao.get(ConfigKeys.COMPANY_INTERNET_SITE).get(0).configValue)
            }
        }
        tsc.addPrint(1, 1) // 打印标签

        tsc.addSound(2, 100) // 打印标签后 蜂鸣器响
        return tsc.command
    }

    private fun getUsbDeviceList() {
        usbDeviceAddresses.clear()
        val manager = MainApplication.getApplication().getSystemService(Context.USB_SERVICE) as UsbManager
        val devices = manager.deviceList
        val deviceIterator: Iterator<UsbDevice> = devices.values.iterator()
        while (deviceIterator.hasNext()) {
            val device = deviceIterator.next()
            if (checkUsbDevicePidVid(device)) {
                usbDeviceAddresses.add(device)
            }
        }
    }

    private fun checkUsbDevicePidVid(dev: UsbDevice): Boolean {
        val pid = dev.productId
        val vid = dev.vendorId
        var rel = false
        if (vid == 34918 && pid == 256 || vid == 1137 && pid == 85
                || vid == 6790 && pid == 30084
                || vid == 26728 && pid == 256 || vid == 26728 && pid == 512
                || vid == 26728 && pid == 256 || vid == 26728 && pid == 768
                || vid == 26728 && pid == 1024 || vid == 26728 && pid == 1280
                || vid == 26728 && pid == 1536) {
            rel = true
        }
        return rel
    }

    private fun registerUSBBroadcastReceiver() {
        val filter = IntentFilter(ACTION_USB_PERMISSION)
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
        filter.addAction(DeviceConnFactoryManager.ACTION_CONN_STATE)
        registerReceiver(mUSBBroadcastReceiver, filter)
    }

    private val sdf1 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    open fun getUUValue(i: Int, business: Business): String? {
        return try {
            if (i == 0) {
                return business.itemValue1
            } else if (i == 1) {
                return business.itemValue2
            } else if (i == 2) {
                return business.itemValue3
            } else if (i == 3) {
                return business.itemValue4
            } else if (i == 4) {
                return business.itemValue5
            } else if (i == 5) {
                return business.itemValue6
            }
            business.itemValue1
        } catch (e: Exception) {
            e.printStackTrace()
            business.itemValue1
        }
    }

     open fun Assemblyprinting(business: Business, columnNames: Array<String>, booleansNames: Array<Boolean>): List<PrintShowBean>? {
        val printShowBeanList: MutableList<PrintShowBean> = ArrayList()
        printShowBeanList.clear()
        for (i in booleansNames.indices) {
            val printShowBean = PrintShowBean()
            if (booleansNames[i]) {
                printShowBean.uuName = columnNames[i]
                val UuNameValueStr = getUUValue(i, business)
                printShowBean.uuNameValue = UuNameValueStr
                printShowBeanList.add(printShowBean)
            }
        }
        return printShowBeanList
    }
}